package com.ruoyi.iot.utils;

import com.google.common.primitives.Shorts;

public class CodeUtil {

    public static byte[] getCrc(byte[] data) {
        int crc = 0xFFFF;
        for (byte datum : data) {
            if (datum < 0) {
                crc ^= (int) datum + 256;
            } else {
                crc ^= datum;
            }
            for (int i = 8; i != 0; i--) {
                if ((crc & 0x0001) != 0) {
                    crc >>= 1;
                    crc ^= 0xA001;
                } else {
                    crc >>= 1;
                }
            }
        }
        return Shorts.toByteArray((short) crc);
    }
}
